﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using System.Security.Principal;

[assembly: InternalsVisibleTo("LynxWiki.Tests")]

namespace LynxWiki.DomainModel
{
    public class TopicAccess
    {
        public static bool DenyRead(IPrincipal user, TopicVersion tv, WikiDirectory wd)
        {
            bool testRead = false;
            List<string> denyReadTopicUsers = null;
            List<string> denyReadTopicRoles = null;
            List<string> denyReadDirUsers = null;
            List<string> denyReadDirRoles = null;
            if (tv != null) {
                denyReadTopicUsers = tv.DenyReadPrivsUser;
                denyReadTopicRoles = tv.DenyReadPrivsRole;
            }

            if (wd != null) {
                denyReadDirUsers = wd.DenyReadPrivsUser;
                denyReadDirRoles = wd.DenyReadPrivsRole;
            }

            if (user == null) {
                //if any users are denied then all anonymous users are also denied
                if (denyReadTopicUsers != null) {
                    if (denyReadTopicUsers.Count > 0) testRead = true;
                }
                if (denyReadTopicRoles != null) {
                    if (denyReadTopicRoles.Count > 0) testRead = true;
                }
                if (denyReadDirUsers != null) {
                    if (denyReadDirUsers.Count > 0) testRead = true;
                }
                if (denyReadDirRoles != null) {
                    if (denyReadDirRoles.Count > 0) testRead = true;
                }
            }
            else if (user != null) {
                if (denyReadTopicUsers != null) {
                    if (denyReadTopicUsers.Contains(user.Identity.Name)) {
                        testRead = true;
                    }
                }
                if (!testRead && denyReadDirUsers != null) {
                    if (denyReadDirUsers.Contains(user.Identity.Name)) {
                        testRead = true;
                    }
                }
                if (!testRead && denyReadTopicRoles != null) {
                    foreach (string role in denyReadTopicRoles) {
                        if (user.IsInRole(role)) {
                            testRead = true;
                        }
                    }
                }
                if (!testRead && denyReadDirRoles != null) {
                    foreach (string role in denyReadDirRoles) {
                        if (user.IsInRole(role)) {
                            testRead = true;
                        }
                    }
                }
            }

            return testRead;
        }

        public static bool CanWrite(IPrincipal user, TopicVersion tv, WikiDirectory wd)
        {
            bool testWrite = false;
            List<string> canWriteTopicUsers = null;
            List<string> canWriteTopicRoles = null;
            List<string> canWriteDirUsers = null;
            List<string> canWriteDirRoles = null;
            if (tv != null) {
                canWriteTopicUsers = tv.AllowUpdatePrivsUser;
                canWriteTopicRoles = tv.AllowUpdatePrivsRole;
            }

            if (wd != null) {
                canWriteDirUsers = wd.AllowUpdatePrivsUser;
                canWriteDirRoles = wd.AllowUpdatePrivsRole;
            }

            if (user == null) {
                if (canWriteTopicUsers != null || canWriteTopicRoles != null || canWriteDirUsers != null || canWriteDirRoles != null) {
                    testWrite = true;
                }
            }
            else if (user != null) {
                if (canWriteTopicUsers != null) {
                    if (canWriteTopicUsers.Contains(user.Identity.Name)) {
                        testWrite = true;
                    }
                }
                if (!testWrite && canWriteDirUsers != null) {
                    if (canWriteDirUsers.Contains(user.Identity.Name)) {
                        testWrite = true;
                    }
                }
                if (!testWrite && canWriteTopicRoles != null) {
                    foreach (string role in canWriteTopicRoles) {
                        if (user.IsInRole(role)) {
                            testWrite = true;
                        }
                    }
                }
                if (!testWrite && canWriteDirRoles != null) {
                    foreach (string role in canWriteDirRoles) {
                        if (user.IsInRole(role)) {
                            testWrite = true;
                        }
                    }
                }
            }

            return testWrite;
        }
    }
}
